package com.yocard.yxyd.ui.presenter;

import com.yocard.yxyd.utils.util.CacheDoubleStaticUtils;
import com.yocard.yxyd.base.CommonSubscriber;
import com.yocard.yxyd.base.Constants;
import com.yocard.yxyd.base.RxPresenter;
import com.yocard.yxyd.http.HttpManager;
import com.yocard.yxyd.http.model.response.CommonBean;
import com.yocard.yxyd.ui.contract.LoginContract;
import com.yocard.yxyd.utils.RxUtil;

import org.json.JSONArray;

import java.util.List;

import javax.inject.Inject;

/**
 * Created on 2019/4/1.
 */
public class LoginPresenter extends RxPresenter<LoginContract.View> implements LoginContract.Presenter {

    @Inject
    public LoginPresenter(HttpManager manager) {
        super(manager);
    }

    @Override
    public void getCode(String phone) {
        addSubscribe(httpManager.getSmsCode(phone)
                .compose(RxUtil.rxSchedulerHelper())
                .subscribeWith(new CommonSubscriber<List<Object>>(mView) {
                    @Override
                    public void onSuccess(List<Object> data) {
                        mView.getCodeSuccess();
                    }
                }));
    }

    @Override
    public void loginUser(String mobile, String code, String longitude, String latitude, String device, String push_token) {
        addSubscribe(httpManager.loginUser(mobile, code, longitude, latitude, device, push_token)
                    .compose(RxUtil.rxSchedulerHelper())
                    .subscribeWith(new CommonSubscriber<CommonBean>(mView){
                        @Override
                        public void onSuccess(CommonBean data) {
                            CacheDoubleStaticUtils.put(Constants.KEY_TOKEN,data.getAccess_token());
                            mView.loginSuccess();
                        }
                    }));
    }
}
